(function(layui) {
  'use strict';

  var MODULE_NAME = 'lang';

  /**
   *  String format template
   *  - Inspired:
   *    https://github.com/Matt-Esch/string-template/index.js
   */
  var RE_NARGS = /(%|)\{([0-9a-zA-Z_]+)\}/g;

  function Format() {
    function hasOwn(obj, key) {
      return Object.prototype.hasOwnProperty.call(obj, key);
    }

    /**
     * template
     *
     * @param {String} string
     * @param {Array} ...args
     * @return {String}
     */

    function template(string) {
      var args = []; var len = arguments.length - 1;
      while (len-- > 0) args[ len ] = arguments[ len + 1 ];

      if (args.length === 1 && typeof args[0] === 'object') {
        args = args[0];
      }

      if (!args || !args.hasOwnProperty) {
        args = {};
      }
      if (string === undefined) {
        return '';
      }

      return string.replace(RE_NARGS, function(match, prefix, i, index) {
        var result;

        if (string[index - 1] === '{' &&
          string[index + match.length] === '}') {
          return i;
        } else {
          result = hasOwn(args, i) ? args[i] : null;
          if (result === null || result === undefined) {
            return '';
          }

          return result;
        }
      });
    }

    return template;
  }

  layui && layui.define(['jquery', 'laytpl'], function(exports) {
    var $ = layui.jquery;
    var laytpl = layui.laytpl;
    var t = Format();

    var Lang = function() {

    };

    Lang.prototype.t = t;

    exports(MODULE_NAME, new Lang());
  });
})(layui);
